package com.synchrona.registrationDataAccess;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import com.synchrona.common.Common;
import com.synchrona.common.EnumUpdateStatus;
import com.synchrona.common.EnumUserStatus;
import com.synchrona.user.User;

public class RegistrationDataAccess {
	private Connection connection = null;
	private CallableStatement cs;
	
	public RegistrationDataAccess() {
		
	}
	
	public void register(User user){
		try {
			connection = getConnection();
			cs = connection.prepareCall("{call spInsertToMaster(?,?}");
			cs.setString(Common.colMasterUserName, user.getName());
			cs.setString(Common.colMasterStatus, EnumUserStatus.Active.name());
			cs.executeUpdate();
			System.out.println("User Registered......");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void updateUserData(User user){
		try {
			connection = getConnection();
			cs = connection.prepareCall("{call spInsertUserData(?,?,?,?}");
			cs.setString(Common.colUserName, user.getName());
			cs.setString(Common.colUserEmail, user.getEmail());
			cs.setObject(Common.colUserRegDate,	user.getRegisteredDate());
			cs.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Connection getConnection(){
		try {
			java.lang.Class.forName("com.microsoft.SQLServerDriver");
			System.out.println("Connecting to the database server for regitration.......");
			connection = DriverManager.getConnection("jdbc:sqlserver://localhost\\SQLEXPRESS;user=sa;password=060056E;databse=Synchrona");
			System.out.println("Connected!......");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return connection;
	}
}
